#include "stdafx.h"


WLActor::WLActor(WLVector3& Loc, WLQuaternion& Rot)
{
	m_Location = Loc;
	m_Rotation = Rot;
}
void WLActor::SetPrimitive(WLPrimitive* Primitive)
{
	this->m_Primitive = Primitive;

}
void WLActor::SetAnimation(class WLAnim* Anim)
{
	this->m_Anim = Anim;
	
}
WLPrimitive* WLActor::GetPrimitive()
{
	return m_Primitive;
}
void WLActor::UpdateAnimation(float DeltaTime)
{
	if ( !m_Anim )
		return;

}
void WLActor::Update(float DeltaTime)
{
	UpdateAnimation(DeltaTime);
}
void WLActor::Render()
{
	WLRenderInfo* pRenderInfo = m_Level->GetRenderInfo(this);
	pRenderInfo->m_Position = this->m_Location;
	pRenderInfo->m_Rotation = this->m_Rotation;
	pRenderInfo->m_ClearFlag = (ECR_CLEARFLAG)(pRenderInfo->m_ClearFlag | ECR_Matrix);
	if ( this->m_Anim )
	{
	}
}

WLActor* WLLevel::CreateActor(WLVector3& Location, WLQuaternion& Rotation)
{
	WLActor* Actor = new WLActor(Location, Rotation);
	Actor->m_Level = this;
	AddActor(Actor);
	return Actor;
}
static unsigned int ActorIDCount = 0;
void WLLevel::AddActor(WLActor* Actor)
{
	Actor->m_ID = ActorIDCount++;
	m_ActorArray.push_back(Actor);
}
void WLLevel::Update(float DeltaTime)
{
	for ( unsigned int i = 0; i<this->m_ActorArray.size(); i++ )
	{
		this->m_ActorArray[i]->Update(DeltaTime);
	}
}

void WLLevel::Render()
{
	for ( unsigned int i = 0; i<m_ActorArray.size(); i++ )
	{
		m_ActorArray[i]->Render();
	}
}
#include"WLRender.h"
WLRenderInfo* WLLevel::GetRenderInfo(WLActor* Actor)
{
	return g_pRender->GetRenderInfo(Actor->m_ID);
}
void WLLevel::CreateRenderInfo(WLActor* Actor)
{
	WLRenderInfo Info;
	Info.m_Revision = Actor->GetPrimitive()->GetReversion();
	Info.m_ActorID = Actor->m_ID;
	Info.m_ResID = Actor->GetPrimitive()->ID;
	Info.m_ClearFlag = ECR_All;
	Info.m_Position = Actor->m_Location;
	Info.m_Rotation = Actor->m_Rotation;
	Info.m_Primitive = Actor->GetPrimitive();
	Info.m_Material = NULL;
	g_pRender->m_RenderInfoArray.push_back(Info);
}

void WLLevel::InitLevel()
{	
	/*
	WLMesh* pMesh =(WLMesh*)(g_pResourceManager->LoadResource(ERT_Mesh,"..\\Res\\Meshes\\3.mesh"));
	pMesh->SetPrimitiveType(PT_TRIANGLELIST);
	WLActor* MeshActor = CreateActor(WLVector3(0,0,0),WLQuaternion(0,0,0,0));
	MeshActor->SetPrimitive(pMesh);
	CreateRenderInfo(MeshActor);
	*/
		
	WLBoneAnim* pBoneAnim =(WLBoneAnim*)(g_pResourceManager->LoadResource(ERT_BoneAnim,"..\\Res\\Meshes\\1.anim"));

	WLBoneMesh* pBoneMesh =(WLBoneMesh*)(g_pResourceManager->LoadResource(ERT_BoneMesh,"..\\Res\\Meshes\\4.mesh"));
	pBoneMesh->SetPrimitiveType(PT_TRIANGLELIST);
	WLActor* MeshActor = CreateActor(WLVector3(0,0,0),WLQuaternion(0,0,0,0));
	MeshActor->SetPrimitive(pBoneMesh);
	MeshActor->SetAnimation(pBoneAnim);
	CreateRenderInfo(MeshActor);

	////g_pLineBatch->SetPrimitiveType(PT_LINELIST);
	////g_pLineBatch->Build();
	////WLActor* LineActor = CreateActor(WLVector3(0,0,0),WLQuaternion(0,0,0,0));
	////LineActor->SetPrimitive(g_pLineBatch);
	////CreateRenderInfo(LineActor);

	WLGrid* pGrid = new WLGrid(10.f,5);
	pGrid->SetPrimitiveType(PT_LINELIST);
	pGrid->Build();
	WLActor* GridActor = CreateActor(WLVector3(0,0,0),WLQuaternion(0,0,0,0));
	GridActor->SetPrimitive(pGrid);
	CreateRenderInfo(GridActor);
}
